package com.jinglu.cloud.common.model.entity;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lvzhenzhao
 * @Datetime 2021/03/02-下午2:27
 */
@Data
public class Result<T> implements Serializable {
    @JsonIgnore
    public static final int DEFAULT_SUCCESS = 200;
    @JsonIgnore
    public static final int DEFAULT_ERROR = 500;
    @JsonProperty(
            value = "code",
            index = 0
    )
    private int code = 200;
    private String message = "";
    private List<ErrorInfo> errors = new ArrayList();
    @JsonProperty(
            value = "data",
            index = 3
    )
    private T data;

    private Result() {
    }

    public static <T> Result<T> success(String message) {
        Result<T> success = new Result();
        success.setCode(200);
        success.setMessage(message);
        return success;
    }

    public static <T> Result<T> success(int code, String message) {
        Result<T> success = new Result();
        success.setCode(code);
        success.setMessage(message);
        return success;
    }

    public static <T> Result<T> success(T data) {
        Result<T> success = new Result();
        success.setCode(200);
        success.setData(data);
        return success;
    }

    public static <T> Result<T> success(int code, T data) {
        Result<T> success = new Result();
        success.setCode(code);
        success.setData(data);
        return success;
    }

    public static <T> Result<T> success(String message, T data) {
        Result<T> success = new Result();
        success.setCode(200);
        success.setMessage(message);
        success.setData(data);
        return success;
    }

    public static <T> Result<T> success(int code, String message, T data) {
        Result<T> success = new Result();
        success.setCode(code);
        success.setMessage(message);
        success.setData(data);
        return success;
    }

    public static <T> Result<T> error(String message) {
        Result<T> error = new Result();
        error.setCode(500);
        error.setMessage(message);
        return error;
    }

    public static <T> Result<T> error(int code, String message) {
        Result<T> error = new Result();
        error.setCode(code);
        error.setMessage(message);
        return error;
    }

    public static <T> Result<T> error(int code, String message, T data) {
        Result<T> error = new Result();
        error.setCode(code);
        error.setMessage(message);
        error.setData(data);
        return error;
    }

    public static Result wrap(boolean flag){
        Result result = new Result<>();
        if(flag){
            result.setCode(200);
            result.setMessage("操作成功");
        } else {
            result.setCode(500);
            result.setMessage("操作失败");
        }
        return result;
    }

    public static Result wrap(boolean flag, String message){
        Result result = new Result<>();
        if(flag){
            result.setCode(200);
            result.setMessage(message);
        } else {
            result.setCode(500);
            result.setMessage(message);
        }
        return result;
    }

    public void addErrorInfo(ErrorInfo errorInfo) {
        this.errors.add(errorInfo);
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

}
